<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('个体工商户批量导入列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>机构名称：</label>
                            <input name="deptId" type="hidden" id="treeId"/>
                            <input name="deptName" onclick="selectDeptTree()" id="treeName" type="text"
                                   placeholder="请选择归属机构" autocomplete="off">
                        </li>
                        <li>
                            <label>姓名：</label>
                            <input type="text" name="name"/>
                        </li>
                        <li>
                            <label>手机号：</label>
                            <input type="text" name="phoneNumber"/>
                        </li>
                        <li>
                            <label>身份证号：</label>
                            <input type="text" name="cardnum"/>
                        </li>
                        <li>
                            <label>门面名称：</label>
                            <input type="text" name="handName"/>
                        </li>
                        <li>
                            <label>管户人：</label>
                            <input type="text" name="userName"/>
                        </li>
                        <li>
                            <label>是否分配：</label>
                            <select name="whetherAllocation" id="whetherAllocation"
                                    th:with="type=${@dict.getType('import_whether_allocation')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"
                                        th:field="${ecardMerchantImport.whetherAllocation}"></option>
                            </select>

                        </li>

                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="imp:merchantImport:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <!--  <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
                 shiro:hasPermission="imp:merchantImport:edit">
                  <i class="fa fa-edit"></i> 修改
              </a>-->
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="imp:merchantImport:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="importExcel()" shiro:hasPermission="imp:merchantImport:import">
                <i class="fa fa-upload"></i> 存量导入
            </a>
            <a class="btn btn-warning" onclick="newImportExcel()" shiro:hasPermission="imp:merchantImport:import">
                <i class="fa fa-upload"></i> 新增导入
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="imp:merchantImport:export">
                <i class="fa fa-download"></i> 导出
            </a>
            <a class="btn btn-primary " onclick="allocation()"
               shiro:hasPermission="imp:merchantImport:defineTaskAllocation">
                <i class="fa fa-edit"></i> 自定义分配
            </a>
            <a class="btn btn-primary" onclick="allocationAllTask()"
               shiro:hasPermission="imp:merchantImport:allTaskAllocation">
                <i class="fa fa-edit"></i> 一键分配
            </a>
            <a class="btn btn-primary" onclick="transfer()" shiro:hasPermission="imp:merchantImport:transfer">
                <i class="fa fa-edit"></i> 转交
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('imp:merchantImport:edit')}]];
    var removeFlag = [[${@permission.hasPermi('imp:merchantImport:remove')}]];
    var commonYesNoDatas = [[${@dict.getType('common_yes_no')}]];
    var whetherRecordDatas = [[${@dict.getType('import_whether_record')}]];

    var prefix = ctx + "ecard/imp/merchantImport";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "个体工商户批量导入",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '姓名'
                },
                {
                    field: 'phoneNumber',
                    title: '手机号'
                },
                {
                    field: 'cardnum',
                    title: '身份证号'
                },
                {
                    field: 'handName',
                    title: '门面名称'
                },
                {
                    field: 'address',
                    title: '客户地址'
                },
                {
                    field: 'licenseName',
                    title: '营业执照名称'
                },
                {
                    field: 'whetherHisCustomer',
                    title: '是否历史客户',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(commonYesNoDatas, value);
                    }
                },
                {
                    field: 'userCode',
                    title: '管户人工号'
                },
                {
                    field: 'userName',
                    title: '管户人'
                },
                {
                    field: 'deptName',
                    title: '归属支行或部门'
                },
                {
                    field: 'corpName',
                    title: '法人机构'
                },
                {
                    field: 'whetherRecord',
                    title: '是否建档',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(whetherRecordDatas, value);
                    }
                },
                {
                    field: 'whetherAllocation',
                    title: '是否分配'
                },
                {
                    field: 'registName',
                    title: '执行人'
                },
                {
                    field: 'registDept',
                    title: '执行人所属机构'
                },
                {
                    field: 'semResult',
                    title: '执行结果'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.whetherAllocation == '未分配') {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    /* 选择机构树 */
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSub
        };
        $.modal.openOptions(options);
    }

    function doSub(index, layero) {
        var body = $.modal.getChildFrame(index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        $.modal.close(index);
    }

    function reset() {
        $('#treeId').val('')
        $.form.reset();
    }

    //打开导入界面
    function importExcel() {
        layer.open({
            type: 2,
            area: [800 + 'px', ($(window).height() - 50) + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: [0.8,'#393D49'],
            title: "导入存量客户信息",
            content: prefix + "/openImportOldCustPage",
            btn: ['确定','关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function (index) {
                $.table.search();
                layer.close(index);
            },
            cancel: function (index) {
                return true;
            }
        });
    }

    //打开导入界面
    function newImportExcel() {
        layer.open({
            type: 2,
            area: [800 + 'px', ($(window).height() - 50) + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "导入新增客户信息",
            content: prefix + "/openImportNewCustPage",
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function (index) {
                $.table.search();
                layer.close(index);
            },
            cancel: function (index) {
                return true;
            }
        });
    }


    //一键分配
    function allocationAllTask() {
        var url = ctx + 'ecard/imp/merchantImport/allocationAll/';
        var options = {
            title: '选择客户经理',
            url: url,
            callBack: doSubmitAllTask
        };
        $.modal.openOptions(options);

    }

    function doSubmitAllTask(index, layero) {
        layero.find("iframe")[0].contentWindow.submitHandler();
        var registIds = $.modal.getChildFrame(index).find('#registIds').val();
        var taskDays = $.modal.getChildFrame(index).find('#taskDays').val();
        var taskNums = $.modal.getChildFrame(index).find('#taskNums').val();
        var allocationType = $.modal.getChildFrame(index).find('#allocationType').val();
        var merchantType = $.modal.getChildFrame(index).find("#merchantType").val();//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        if (allocationType != 1 && allocationType != 2) {
            $.modal.alert("请选择分配方式！");
            return;
        }
        if (allocationType == 2 && taskNums == "") {
            $.modal.alert("请输入任务数量！");
            return;
        }
        if (registIds == "") {
            $.modal.alert("请选择营销经理！");
            return;
        }
        if (taskDays == "") {
            $.modal.alert("请输入任务期限！");
            return;
        }
        var data = {
            "merchantType": merchantType,
            "registIds": registIds,
            "taskDays": taskDays,
            "taskNums": taskNums,
            "allocationType": allocationType
        };
        $.modal.confirm("确认要分配 " + taskNums + " 个任务吗?", function () {
            $.ajax({
                type: "post",
                url: ctx + "ecard/imp/merchantImport/allTaskAllocation",
                async: true,
                dataType: 'json',
                data: data,
                success:
                    function (r) {
                        if (r.code == web_status.SUCCESS) {
                            setTimeout("$.table.refresh()", 600);
                            $.modal.close(index);
                            $.modal.alertWarning(r.msg);
                        } else {
                            $.modal.alertWarning(r.msg);
                        }
                    }
            });
        });

    }

    //自定义分配
    function allocation() {
        var rows = $("#bootstrap-table").bootstrapTable('getSelections');
        var url = ctx + 'ecard/imp/merchantImport/allocation/';
        var options = {
            title: '选择客户经理',
            url: url,
            callBack: doSubmitTask
        };
        if (rows.length == 0) {
            $.modal.alert("请选择客户！");
            return;
        }
        $.modal.confirm("确认要分配选中的【" + rows.length + "】个客户吗?", function () {
            $.modal.openOptions(options);
        });

    }

    function doSubmitTask(index, layero) {
        layero.find("iframe")[0].contentWindow.submitHandler();
        var id = $.table.selectFirstColumns();//需要分配的个体工商户id
        var merchantType = $.modal.getChildFrame(index).find("#merchantType").val();//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        var registIds = $.modal.getChildFrame(index).find("#registIds").val();//客户经理微信端注册id
        var deptId = $.modal.getChildFrame(index).find("#deptId").val();//客户经理所在部门id
        var taskDays = $.modal.getChildFrame(index).find('#taskDays').val();
        if (registIds == "") {
            $.modal.alert("请选择营销经理！");
            return;
        }
        if (taskDays == "") {
            $.modal.alert("请输入任务期限！");
            return;
        }

        var data = {
            "ids": id.join(),
            "merchantType": merchantType,
            "registIds": registIds,
            "deptId": deptId,
            "taskDays": taskDays
        };
        $.operate.submit(ctx + "ecard/imp/merchantImport/defineTaskAllocation", "post", "json", data);
        $.modal.close(index);

    }

    //转交
    function transfer() {
        var rows = $("#bootstrap-table").bootstrapTable('getSelections');
        var url = ctx + 'ecard/imp/merchantImport/transfer/';
        var options = {
            title: '选择客户经理',
            url: url,
            callBack: doSubmitTransfer
        };
        if (rows.length == 0) {
            $.modal.alert("请选择客户！");
            return;
        }
        $.modal.confirm("确认要转交选中的【" + rows.length + "】个客户吗?", function () {
            $.modal.openOptions(options);
        });

    }

    function doSubmitTransfer(index, layero) {
        layero.find("iframe")[0].contentWindow.submitHandler();
        var id = $.table.selectFirstColumns();//需要分配的个体工商户id
        var type = $.modal.getChildFrame(index).find("#type").val();//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        var registIds = $.modal.getChildFrame(index).find("#registIds").val();//客户经理微信端注册id
        var deptId = $.modal.getChildFrame(index).find("#deptId").val();//客户经理所在部门id
        if (registIds == "") {
            $.modal.alert("请选择营销经理！");
            return;
        }

        var data = {
            "ids": id.join(),
            "type": type,
            "registIds": registIds,
            "deptId": deptId
        };
        $.operate.submit(ctx + "ecard/imp/merchantImport/doTransfer", "post", "json", data);
        $.modal.close(index);

    }


</script>
</body>
</html>